package com.runvp.netty.server.handler;


import com.alibaba.fastjson.JSONObject;
import com.runvp.common.enums.NettyCmd;
import com.runvp.common.model.tcp.MessageReceiveBody;
import com.runvp.common.model.tcp.MessageResBody;
import com.runvp.common.util.ToolUtils;
import com.runvp.netty.constant.NettyConstant;
import com.runvp.service.build.BuildStaticService;
import com.runvp.service.out.OutHandler;
import com.runvp.service.tcp.TcpService;
import io.netty.channel.Channel;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class BizHandle {


    @Setter
    private Channel channel;
    @Setter
    private MessageReceiveBody parseBody;
    @Autowired
    private TcpService tcpService;
    @Autowired
    private OutHandler outHandler;
    /**
     * 业务处理
     *
     * @return 返回指令
     */
    public MessageResBody handle() {
        MessageResBody cmd = null;
        String type = parseBody.getType();
        String cmdKey = parseBody.getCmd();
        //错误报文结合
        String tcpErrorMessage = BuildStaticService.configService.selectConfigByKey("tcpErrorMessage");
        List<String> errors = ToolUtils.strToList(tcpErrorMessage);
        if(errors.contains(parseBody.getSourceMsg())){
            //语音
            log.error("2-错误报文不处理,清理当前通道");
            NettyConstant.removeChannel(channel);
            return null;
        }
        NettyCmd exeCmd = NettyCmd.build(type,cmdKey);
        if (null == exeCmd) {
            log.warn("2-本次执行命令type:{},cmd:{},未知命令,执行结束 ", type,cmdKey);
            return null;
        }
        log.info("2-本次接收命令type:{},cmd:{} ,exeCmd:{} ", type,cmdKey,exeCmd.getDesc());
        try {
            switch (exeCmd) {
                //设备应答端口可充电状态
                case DEVICE_RES_PORT_RECHARGEABLE_STATUS:
                    cmd = new DeviceResQueryChargePortStatusHandle(tcpService, parseBody).handle(channel);
                    break;
                //启动充电应答
                case DEVICE_CHARG_START_RES:
                    cmd = new DeviceResStartChargeHandle(tcpService, parseBody).handle(channel);
                    break;
                //停止充电应答
                case DEVICE_CHARG_STOP_RES:
                    cmd = new DeviceResStopChargeHandle(tcpService, parseBody).handle(channel);
                    break;
                //设备充电结束上报回应
                case CHARG_END_REPORT:
                    cmd = new DeviceReqStopChargeHandle(tcpService, parseBody).handle(channel);
                    break;
                //设备应答实时信息
                case DEVICE_RES_REAL_TIME_INFO:
                    cmd = new DeviceResRealTimeInfoHandle(tcpService, parseBody).handle(channel);
                    break;
                //服务器设置设备工作参数解析
                case SERVER_CONFIG_DEVICE_WORK_PARAM:
                    cmd = new ServerResWorkParamHandle(tcpService, parseBody).handle(channel);
                    break;
                //设备应答收费方案
                case DEVICE_RES_FEE_SCHEME:
                    cmd = new DeviceResFeeSchemeHandle(tcpService, parseBody).handle(channel);
                    break;
                    //设备应答远程重启
                case DEVICE_RES_REMOTE_RESTART:
                    cmd = new DeviceResRemoteRestartHandle(tcpService, parseBody).handle(channel);
                    break;
                case LOGIN:
                    //设备请求登录
                    cmd = new LoginHandle(tcpService, parseBody).handle(channel);
                    break;
                case DEVICE_REPORT_BASE_INFO:
                    //设备上报基本信息
                    cmd = new DeviceReportBaseInfoHandle(tcpService, parseBody).handle(channel);
                    break;
                case DEVICE_TIMING_REPORT_REAL_TIME_INFO:
                    //设备定时上报实时信息
                    cmd = new DeviceTimingReportRealTimeInfoHandle(tcpService, parseBody).handle(channel);
                    break;
                case CHARGING_REAL_TIME_DATA_REPORT:
                    //充电过程中实时数据上报
                    cmd = new DeviceChargeTimingReportRealTimeInfoHandle(tcpService, parseBody).handle(channel);
                    break;
                case DEVICE_REQ_FEE_SCHEME:
                    //设备请求收费方案
                    cmd = new FeeSchemeHandle(tcpService, parseBody).handle(channel);
//                    cmd = JSONObject.parseObject("{\"body\":\"6BE31626EF611A40CBEC38A4928ACF6CB0EBD254CC246227C54FA243F7D271F95BBCD70B9E133BBE1898C11AA2A03E9D00379282B48F8F7A152A31B016372E2BC2E781B119D09CDFA1835789BD46CE3D6C03BB3A6D3153AFEB08B40756D32071A79A9448FF27AE34BD7B90BED558BC7AA94037896D1D61EF\",\"bodyJson\":{\"pr3\":\"0.7\",\"pr2\":\"0.7\",\"pr5\":\"0.8\",\"pr4\":\"0.7\",\"pr7\":\"1\",\"pr6\":\"0.9\",\"pr10\":\"1.3\",\"pr9\":\"1.2\",\"pr8\":\"1.1\",\"pr1\":\"0.7\"},\"cmd\":\"02\",\"did\":\"000000000047\",\"head\":\"3A3A\",\"length\":\"0078\",\"tag\":\"3CCF1F01E8C9787258D8C322EA54EE78\",\"time\":1718462431341,\"type\":\"04\",\"version\":\"01\"}\n",MessageResBody.class);

                    break;
                case XL:
                    cmd = new XLHandle(tcpService, parseBody).xl(channel);
                    break;
                case DEVICE_REQ_CARD_BALANCE:
                    //设备刷卡请求余额
                    cmd = new DeviceSwipeCardBalanceHandle(tcpService, parseBody).handle(channel);
                    break;
                case DEVICE_SWIPE_CARD_REQ_CHARGE:
                    //设备刷卡充电
                    cmd = new DeviceSwipeCardChargeHandle(tcpService, parseBody).handle(channel);
                    break;
                default:
                    //未知命令返回默认报文
                    break;
            }
        } catch (Exception e) {
            //未知命令返回默认报文
//            cmd = new XLHandle(tcpService, parseBody).defaultRes();
            log.error("2-指令处理发生异常: {}", e.getMessage());
        }
        log.info("2-本次命令响应报文组装完成:{}", null == cmd ?exeCmd.getDesc()+": 无需回应":JSONObject.toJSONString(cmd));
        return cmd;
    }


}
